angular.module('locals', [])
    .factory('ls', ['$window', function ($window) {
        return {
            set: function (key, value) {
                $window.localStorage[key] = value;
            },
            get: function (key, defaultValue) {
                return $window.localStorage[key] || defaultValue;
            },
            setObject: function (key, value) {
                $window.localStorage[key] = JSON.stringify(value);
            },
            getObject: function (key) {
                return JSON.parse($window.localStorage[key] || '{}');
            }
        }
    }]).factory('commService', ['$ionicPopup', '$http', '$rootScope', '$ionicLoading', function ($ionicPopup, $http, $rootScope, $ionicLoading) {
        return {
            ajax: function (type, url, param, success) {
                this.doAjax(type, $rootScope.baseurl + url, param, success);
            },
            doAjax: function (type, url, param, success) {
                var para = {
                    method: type,
                    url: url,
                    data: param,
                    params: param
                }
                var promis = $http(para);
                var showAlert = this.showAlert;
                var hide = this.hide;
                promis.success(success)
                promis.error(function (error) {
                    showAlert('操作失败！', error);
                    hide();
                });
            },
            showAlert: function (title, template) {
                var alertPopup = $ionicPopup.alert({
                    title: title,
                    template: template
                });
                alertPopup.then(function (res) {
                    console.log('showAlert.....');
                });
            },
            show: function () {
                $ionicLoading.show({
                    template: 'Loading...'
                });
            },
            hide: function () {
                $ionicLoading.hide();
            },
            isDigit: function (s) {
                var patrn = /^1[0-9]{10}$/;
                if (!patrn.exec(s)) return false
                return true
            }

        }
    }]);